import { PageList } from "@/api/menu";
import { treeList } from "@/api/area";
import { keyList as  dictDataPage} from "@/api/dictData";
import { PageList as officeTree,TreeDataOffice } from "@/api/office";
import { listCompanyActivities } from "@/api/crm/activity";

import { listToTree ,areaPageListToTreeForId,officeUserListToTree} from "@/utils/tree";


export default {
  props: {
    treeType: {
      type: String,
      default: ""
    },
    queryObj:{
      type:Object,
      default:()=>new Object()
    }
  },
  mounted() {
    console.log("搜索选择组件————挂载mounted")
  },
  beforeUpdate() {
    console.log("搜索选择组件————beforeUpdate")
  },
  updated() {
    console.log("搜索选择组件————updated")
  },
  methods: {
    CheckTreeData() {
      switch (this.treeType) {
        case "menu":this.LoadMenuTree();break;
        case "dictData":this.LoadDictData();break;
        case "areaTree":this.LoadAreaTree();break;
        case "officeUser":{
          let keyIncludeObj = {
            include:"_user",
            message:"请选择用户"
          };
          Object.assign(this,{
            keyIncludeObj,
            defaultExpandAll:true
          })
          this.LoadOfficeUserTree();break;
        };
        case "office":this.LoadOffice();break;
        case "activity":this.LoadActivity();break;
      }
    },

    // 加载 菜单树
    LoadMenuTree() {
      const postData = {
        "page.current": 1,
        "page.size": -1,
        isAll:true,
      };
      PageList(postData)
        .then(response => {
          if (response.code === 200) {
         
            const { data } = response;
            const { records } = data;
            let filterData = records.filter(f=>f.menuType != 2);
            const childrenNav = [];
           
            listToTree(filterData, childrenNav, 0, {
              key: "id",
              title: "menuName"
            });
            this.treeData = childrenNav;
            console.log("获取菜单列表_treeData", this.treeData);
            this.generateList(childrenNav);
            console.log("获取菜单列表_dataList", this.dataList);
            if(childrenNav.length == 0){

            }
          }
        })
        .catch(error => {});
    },
    //加载 字典类型树
    LoadDictData(){
      const {dictTypeId} = this.queryObj;
      const postData = {
        "page.current": 1,
        "page.size": -1,
        dictTypeId
      };
      dictDataPage(postData)
        .then(response => {
          if (response.code === 200) {
            const { data } = response;
            const { records } = data;
            const childrenNav = [];
            listToTree(records, childrenNav, 0, {
              key: "id",
              title: "dictLabel"
            });
            this.treeData = childrenNav;
            console.log("获取字典数据_treeData", this.treeData);
            this.generateList(childrenNav);
            console.log("获取字典数据_dataList", this.dataList);
          }
        })
        .catch(error => {});
    },
    //加载 区域树
    LoadAreaTree(){
      const postData = {
        current: 1,
        size: -1,
      };
      treeList(postData)
        .then(response => {
          if (response.code === 200) {
            
            const { data } = response;
            const childrenNav = [];

            areaPageListToTreeForId(data, childrenNav, 0);
            this.treeData = childrenNav;
            console.log("获取地区数据_treeData", this.treeData);
            this.generateList(childrenNav);
            console.log("获取地区数据_dataList", this.dataList);

           
          }
        })
        .catch(error => {});
    },
    //加载 组织人员树
    LoadOfficeUserTree(){
      TreeDataOffice({ isLoadUser: 2 }).then(response => {
        const { data } = response;
        const childrenNav = [];
        officeUserListToTree(data, childrenNav, "0");
        this.treeData = childrenNav;
        console.log("获取组织人员数据_treeData", this.treeData);
        this.generateList(childrenNav);
        console.log("获取组织人员数据_dataList", this.dataList);
      });
    },
    LoadOffice(){

      TreeDataOffice({ isLoadUser: 0 }).then(response => {
        const { data } = response;
        const childrenNav = [];
        officeUserListToTree(data, childrenNav, "0");
        this.treeData = childrenNav;
        console.log("获取组织人员数据_treeData", this.treeData);
        this.generateList(childrenNav);
        console.log("获取组织人员数据_dataList", this.dataList);
      });
    },
    /**
     * 加载市场活动
     */
    LoadActivity(){
      listCompanyActivities().then(response => {
        if (response.code === 200) {
          const { data } = response;
          this.treeData = data;
          this.generateList(data);
        }
      })
      .catch(error => {});
    }
  }
};
